package com.emily.test.service.impl;

import com.emily.test.mapper.TCompanyInfoMapper;
import com.emily.test.mapper.TCompanyMapper;
import com.emily.test.model.TCompany;
import com.emily.test.model.TCompanyInfo;
import com.emily.test.service.TCompanyService;
import com.emily.test.utils.ConstentUtil;
import com.emily.test.utils.DateUtil;
import com.emily.test.vo.CompanyVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

@Service(value = "tCompanyAgent")
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
public class TCompanyImpl implements TCompanyService {

    private Logger logger = LoggerFactory.getLogger(TResourceServiceImpl.class);

    @Resource
    private TCompanyMapper tCompanyMapper;
    @Resource
    private TCompanyInfoMapper tCompanyInfoMapper;

    @Override
    public TCompany getCompanyByCode(String code) {
        if(code == null){
            return null;
        }
        return tCompanyMapper.getCompanyByCode(code);
    }

    @Override
    public int insertTCompanyInfo(CompanyVO companyVO) {
        TCompany code = tCompanyMapper.getCompanyByCode(companyVO.getCompanyCode());
        if(code != null){
            return 0;
        }
        TCompany tCompany = new TCompany();
        TCompanyInfo tCompanyInfo = new TCompanyInfo();
        String time = DateUtil.getNowStr();
        tCompany.setCompanyCode(companyVO.getCompanyCode());
        tCompany.setcName(companyVO.getCName());
        tCompany.setUpdateTime(time);
        tCompanyInfo.setCompanyCode(companyVO.getCompanyCode());
        tCompanyInfo.setPhone(companyVO.getPhone());
        tCompanyInfo.setEmail(companyVO.getEmail());
        tCompanyInfo.setLegalPerson(companyVO.getLegalPerson());
        tCompanyInfo.setUpdateTime(time);
        tCompanyInfo.setRegistrationTime(time);
        int num1 = tCompanyInfoMapper.insertSelective(tCompanyInfo);
        int num2 = tCompanyMapper.insertSelective(tCompany);
        return num1 + num2;
    }

    @Override
    public int updateTCompany(CompanyVO companyVO) {
        String time = DateUtil.getNowStr();
        String code = companyVO.getCompanyCode();
        TCompany tCompany = tCompanyMapper.getCompanyByCode(code);
        TCompanyInfo tCompanyInfo = tCompanyInfoMapper.selectInfoByCode(code);
        tCompany.setCompanyCode(companyVO.getCompanyCode());
        tCompany.setcName(companyVO.getCName());
        tCompany.setUpdateTime(time);
        tCompanyInfo.setPhone(companyVO.getPhone());
        tCompanyInfo.setEmail(companyVO.getEmail());
        tCompanyInfo.setLegalPerson(companyVO.getLegalPerson());
        tCompanyInfo.setUpdateTime(time);
        tCompanyInfoMapper.update(tCompanyInfo);
        return tCompanyMapper.updateCompany(tCompany);
    }

    @Override
    public int updateTCompany(TCompany tCompany) {
        return tCompanyMapper.updateCompany(tCompany);
    }

    @Override
    public int deleteTCompanyById(int id) {
        return tCompanyMapper.deleteTCompanyById(id);
    }

    @Override
    public List<TCompany> getCompanyList(TCompany tCompany) {
        if (!ConstentUtil.isEmpty(tCompany)) {
            if (ConstentUtil.isEmpty(tCompany.getState())) {

                return tCompanyMapper.getCompanyListByConditionWithOutDel(tCompany);
            } else {
                return tCompanyMapper.getCompanyListByConditionWithInDel(tCompany);
            }
        }
        return null;
    }

    @Override
    public List<CompanyVO> getCompanyList() {
        return tCompanyMapper.getCompanyList();
    }

    @Override
    public CompanyVO selectCompanyInfoById(int id) {
        TCompany tCompany = tCompanyMapper.selectCompanyInfoById(id);
        TCompanyInfo tCompanyInfo = tCompanyInfoMapper.selectInfoByCode(tCompany.getCompanyCode());
        CompanyVO vo = new CompanyVO();
        vo.setId(tCompany.getId());
        vo.setCName(tCompany.getcName());
        vo.setCompanyCode(tCompany.getCompanyCode());
        vo.setEmail(tCompanyInfo.getEmail());
        vo.setLegalPerson(tCompanyInfo.getLegalPerson());
        vo.setPhone(tCompanyInfo.getPhone());
        return vo;
    }

    @Override
    public int updateTCompanyById(List<TCompany> tCompanyList) {
        return tCompanyMapper.updateTCompanyById(tCompanyList);
    }

    @Override
    public List<TCompany> getCompanyListNoRoles() {
        return tCompanyMapper.getCompanyListNoRoles();
    }

    @Override
    public List<TCompany> selectTCompanyWithOutResource() {
        return tCompanyMapper.selectTCompanyWithOutResource();
    }
}
